package com.example.springboot01.controller;

import com.example.springboot01.annotation.WithoutAuthorization;
import com.example.springboot01.config.ResultMsg;
import com.example.springboot01.config.ResultStatusEnum;
import com.example.springboot01.config.RoleTypeEnum;
import com.example.springboot01.config.ServiceException;
import com.example.springboot01.constant.Constants;
import com.example.springboot01.service.ManageAccountService;
import com.example.springboot01.service.TokenManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *管理用户登陆管理控制器
 * @Auther: zrd
 * @Date: 2022/6/3/06/03/19:25
 * @Description: com.example.springboot01.controller
 */


@Api(value = "adminToken",tags = "登陆管理（后台）")
@RestController
@RequestMapping("/admin/token")
public class AdminTokenController {

    @Autowired
    ManageAccountService manageAccountService;
    @Autowired
    TokenManageService tokenManageService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @ApiOperation(value = "登录" ,notes = "管理用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminName" , value = "管理用户名" , required = true,dataType = "String"),
            @ApiImplicitParam(name = "password" , value = "密码" , required = true,dataType = "String")
    })
    @WithoutAuthorization
    @PostMapping("/")
    ResultMsg<String> login(@RequestParam String adminName , @RequestParam String password){
        try {
            //验证用户名和密码
            String userId = manageAccountService.getUserIdByAdminNameAndPassword(adminName,password);
            //创建token，返回前端
            String token = tokenManageService.createToken(userId, RoleTypeEnum.ADMIN.getName());
            if(StringUtils.isEmpty(token)){
                logger.error("token生成为空");
                return ResultMsg.error(ResultStatusEnum.LOGIN_ERROR);
            }
            return ResultMsg.ok(ResultStatusEnum.LOGIN_SUCCESS,token);
        }catch (ServiceException se){
            logger.error("登陆失败：" + se.getMsg());
            return ResultMsg.error(ResultStatusEnum.LOGIN_ERROR,se.getMsg());
        }catch (Exception e) {
            logger.error(ExceptionUtils.getStackTrace(e));
            return ResultMsg.error(ResultStatusEnum.LOGIN_ERROR);
        }
    }

    @ApiOperation(value = "注销",notes = "管理用户注销")
    @ApiImplicitParam(name = "token",value = "token字符串",required = true,dataType = "String",paramType = "header")
    @DeleteMapping("/")
    ResultMsg logout(@RequestHeader String token){

        try {
            //销毁token
            if (!tokenManageService.deleteToken(token)){
                logger.error("删除token失败");
                return ResultMsg.error(ResultStatusEnum.LOGOUT_ERROR);
            }
            return ResultMsg.ok(ResultStatusEnum.LOGOUT_SUCCESS);
        }catch (ServiceException se){
            logger.error("注销失败：" + se.getMsg());
            return ResultMsg.error(ResultStatusEnum.LOGOUT_ERROR);
        }catch (Exception e) {
            logger.error(ExceptionUtils.getStackTrace(e));
            return ResultMsg.error(ResultStatusEnum.LOGOUT_ERROR);
        }
    }
}

